package com.ydy.netty.controller;

import com.ydy.common.BaseController;
import com.ydy.common.model.ChatRecord;
import com.ydy.common.model.User;
import com.ydy.common.model.UserFriend;
import com.ydy.common.util.ParamsUtil;
import com.ydy.common.vo.UserFriendVo;
import com.ydy.netty.service.NettyService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class NettyController extends BaseController {

    @Autowired
    private NettyService nettyService;

    @GetMapping("/getWebSocketUri")
    public Object getWebSocketUri(){
        return retResultVo(nettyService.getWebSocketUrl());
    }

    /**
     * 查询界面信息
     * @Title: getChatUserInfo
     * @author: dy.yin 2021/4/29 11:25
     * @param: [request]
     * @return: java.lang.Object
     * @throws
     */
    @GetMapping("/getChatUserInfo")
    public Object getChatUserInfo(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");
        Map<String,Object> retMap = new HashedMap<>();

        User user = nettyService.queryUserInfo(userCode);
        List<UserFriendVo> list = nettyService.getCanAddFriendList(userCode);
        List<UserFriendVo> friendRequestList = nettyService.queryAddFriendRequestList(userCode);

        retMap.put("user",user);
        retMap.put("canAddFriendList",list);
        retMap.put("friendAddRequestList",friendRequestList);

        return retResultVo(retMap);
    }


    /**
     * 上传头像
     * @Title: headPortraitImageUpload
     * @author: dy.yin 2021/4/26 15:48
     * @param: [file, userCode]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping(value = "/headPortraitImageUpload")
    public Object headPortraitImageUpload(@RequestParam(value = "file", required = false) MultipartFile file,
                                          @RequestParam(value = "userCode") String userCode,
                                          @RequestParam(value = "nickName") String nickName){
        User user = nettyService.headPortraitImageUpload(file, userCode,nickName);
        return retResultVo(user);
    }

    /**
     * 查询可以添加好友的用户
     * @param request
     * @return
     */
    @GetMapping("/getCanAddFriendList")
    public Object getCanAddFriendList(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");
        List<UserFriendVo> list = nettyService.getCanAddFriendList(userCode);
        return  retResultVo(list);
    }

    /**
     * 添加好友
     * @return
     */
    @PostMapping("/addFriend")
    public Object addFriend(@RequestBody LinkedHashMap<String,Object> map){
        String friendCode = MapUtils.getString(map,"friendCode");
        String userCode = MapUtils.getString(map,"userCode");
        nettyService.addFriend(userCode,friendCode);
        return retResultVo();
    }

    /**
     * 查询添加好友申请请求
     * @Title: queryAddFriendRequestList
     * @author: dy.yin 2021/4/29 11:18
     * @param: [request]
     */
    @GetMapping("/queryAddFriendRequest")
    public Object queryAddFriendRequestList(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");
        List<UserFriendVo> friendRequestList = nettyService.queryAddFriendRequestList(userCode);
        return retResultVo(friendRequestList);
    }

    /**
     * 同意好友申请
     * @Title:
     * @author: dy.yin 2021/4/29 11:17
     */
    @PostMapping("/agreeAddFriend")
    public Object agreeAddFriend(@RequestBody UserFriend userFriend){
        nettyService.agreeAddFriend(userFriend);
        return retResultVo();
    }


    /*********************/

    /**
     * 查询聊天界面信息
     * @param request
     * @return
     */
    @GetMapping("/getChatInfo")
    public Object getChatInfo(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");

        User user = nettyService.queryUserInfo(userCode);
        List<UserFriendVo> friendList = nettyService.getUserFriendsList(userCode);
        Map<String,List<ChatRecord>> chatRecordMap = nettyService.queryChatRecord(userCode);

        Map<String,Object> retMap = new HashMap<>();
        retMap.put("friendChatList",friendList);
        retMap.put("user",user);
        retMap.put("chatRecord",chatRecordMap);
        return retResultVo(retMap);
    }

    /**
     * 查询聊天好友列表
     * @param request
     * @return
     */
    @GetMapping("/getChatFriendsList")
    public Object getChatFriendsList(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");
        List<UserFriendVo> list = nettyService.getUserFriendsList(userCode);
        return retResultVo(list);
    }

    /**
     * 获得好友列表
     * @Title: getFriendList
     * @author: dy.yin 2021/4/29 17:07
     * @param: [request]
     */
    @GetMapping("/getFriendList")
    public Object getFriendList(HttpServletRequest request){
        Map<String,Object> map = ParamsUtil.getParams(request);
        String userCode = MapUtils.getString(map,"userCode");
        List<UserFriendVo> list = nettyService.getFriendList(userCode);
        return retResultVo(list);
    }

    /**
     * 添加群组
     * @Title: addGroupChat
     * @author: dy.yin 2021/4/29 15:36
     * @param: [list]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/addGroupChat")
    public Object addGroupChat(@RequestBody List<UserFriend> list){
        nettyService.addGroupChat(list);
        return retResultVo();
    }
}
